package me.raih.controller.admin.common;

import me.raih.pojo.po.User;
import me.raih.pojo.po.Wares;
import me.raih.pojo.vo.PageBean;
import me.raih.pojo.vo.ResultInfo;
import me.raih.service.UserService;
import me.raih.service.WaresService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author Raih
 * @Time 2020/11/28  17:29
 *
 * 关于分页的一些策略
 * 包括 商品分页和用户分页
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminPageController {

    @Autowired
    WaresService waresService;
    //@Autowired
    //PageBean pageBean;
    @Autowired
    ResultInfo resultInfo;
    @Autowired
    UserService userService;

    @RequestMapping(value = "/wares/pageWaresQuery",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo pageDeal(HttpSession session,
                               @Param("currentPage") String currentPage,
                               @Param("param") String param,
                               @Param("bookName") String bookName,
                               @Param("no") String no,
                               @Param("modifyMan") String modifyMan,
                               @Param("curr") String curr,
                               @Param("status") String status,
                               Model model){

        int pageSize = 10;

        if(!"预警".equals(status)&& !"正常".equals(status)){
            status = null;
        }

        PageBean<Wares> waresByPage;
        //如果有当前页码，表示不是搜索就是翻页
        if(currentPage != null){
            //如果是来翻页的，强转一下
            int oldCurrentPage;
            try {
                oldCurrentPage = Integer.parseInt(currentPage);
            } catch (NumberFormatException e) {
                //e.printStackTrace();
                oldCurrentPage = 0;
            }
            //如果此时向前翻页
            if("forward".equals(param)){
                //判断是不是首页，如果是就不要跳转
                if(oldCurrentPage == 1){
                    oldCurrentPage = 2;
                }
                waresByPage = waresService.findWaresByPageAndName(oldCurrentPage-2,pageSize,bookName,no,modifyMan,status);
                //最前页
            }else if("begin".equals(param)){
                waresByPage = waresService.findWaresByPageAndName(0, pageSize,bookName,no,modifyMan,status);
                //最后页
            }else if("end".equals(param)){
                try {
                    oldCurrentPage = Integer.parseInt(curr.replace("页","").replace("共",""));
                    waresByPage = waresService.findWaresByPageAndName((oldCurrentPage-1), pageSize,bookName,no,modifyMan,status);
                } catch (NumberFormatException e) {
                    //e.printStackTrace();
                    oldCurrentPage = 0;
                    waresByPage = waresService.findWaresByPageAndName((oldCurrentPage), pageSize,bookName,no,modifyMan,status);
                }
                //下一页
            }else if("nextPage".equals(param)){
                try {
                    int i = Integer.parseInt(curr.replace("页","").replace("共",""));
                    //如果当前页不是最大页
                    if(i != oldCurrentPage){
                        waresByPage = waresService.findWaresByPageAndName((oldCurrentPage),pageSize,bookName,no,modifyMan,status);
                    }else {
                        waresByPage = waresService.findWaresByPageAndName(oldCurrentPage-1, pageSize,bookName,no,modifyMan,status);
                    }
                } catch (NumberFormatException e) {
                    //e.printStackTrace();
                    //oldCurrentPage = 0;
                    waresByPage = waresService.findWaresByPageAndName(0, pageSize,bookName,no,modifyMan,status);
                }
            }
            //如果是搜寻
            else if("search".equals(param)){
                waresByPage = waresService.findWaresByPageAndName(0, pageSize,bookName,no,modifyMan,status);
                //保存这些个查找的数据

            }else {
                //如果有些问题直接查询第一页
                waresByPage = waresService.findWaresByPageAndName(0, pageSize,bookName,no,modifyMan,status);
            }
        }
        //如果说是第一次进入
        else {
            waresByPage = waresService.findWaresByPageAndName(0,pageSize,bookName,no,modifyMan,status);
        }

        model.addAttribute("WARE_PAGE_LIST",waresByPage);

        resultInfo.setData(waresByPage);

        return resultInfo;
    }

    /**
     *
     * @param curr 最大页
     * @param currentPage   当前页
     * @param param     分页参数
     * @param username  用户名条件
     * @param name  姓名
     * @param email 邮箱
     * @param address          地址
     * @param phone 电话
     * @return 分页的结果集
     */
    @RequestMapping(value = "/user/pageUserQuery",method = RequestMethod.PUT)
    @ResponseBody
    public ResultInfo toPageUser(@RequestParam("curr") String curr,
                                 @RequestParam("currentPage") String currentPage,
                                 @RequestParam("param") String param,
                                 @RequestParam("username") String username,
                                 @RequestParam("name")String name,
                                 @RequestParam("email")String email,
                                 @RequestParam("address")String address,
                                 @RequestParam("phone")String phone){

        //System.out.println("参数是："+param);

        PageBean<User> pageBean;

        if(curr != null){
            curr = curr.replace("共", "").replace("页","");
        }

        int pageSize = 10;
        resultInfo.setFlag(true);
        //如果是首次进入页面
        if(curr == null || currentPage == null){
            pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,0,0,pageSize);
        }
        //不是首次进入时候
        else {
            int intCurrentPage;
            try {
                intCurrentPage = Integer.parseInt(currentPage);
            }catch (Exception e){
                intCurrentPage = 0;
            }
            long longPhone;
            try {
                longPhone = Long.parseLong(phone);
            }catch (Exception e){
                longPhone = 0;
            }
            //以分页的形式判断
            if("start".equals(param)){
                //如果是想去首页
                pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,0,pageSize);
            }else if("forward".equals(param)){
                //如果是上一页，判断是否已经是首页
                if(intCurrentPage == 0){
                    pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,0,pageSize);
                }else {
                    pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,intCurrentPage-1,pageSize);
                }
            }else if("next".equals(param)){
                //如果是下一页
                int intCurr;
                try {
                    intCurr = Integer.parseInt(curr);
                }catch (Exception e){
                    intCurr = 0;
                }
                //判断是否是尾页
                if(intCurrentPage == intCurr){
                    pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,intCurrentPage,pageSize);
                }else {
                    pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,intCurrentPage+1,pageSize);
                }
            }else if("end".equals(param)){
                //如果说是最后一页
                int intCurr;
                try {
                    intCurr = Integer.parseInt(curr);
                }catch (Exception e){
                    intCurr = 0;
                }
                pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,intCurr,pageSize);
            }else if("search".equals(param)){
                //如果是搜寻
                pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,longPhone,0,pageSize);
            }
            else {
                //如果说有什么没考虑到的问题，就查首页吧
                pageBean = userService.findUserByCurrAndCurrentPage(username,name,email,address,0,0,pageSize);
            }
        }

        resultInfo.setData(pageBean);

        return resultInfo;

    }

}
